package com.popo.xml;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

public class XmlPullTest {
	public static void main(String[] args) {
		LinkedList<Data> stack = null;

		Data parent = null;
		Data self = null;
		Data root = null;
		try {
			

			XmlPullParserFactory f = XmlPullParserFactory.newInstance();
			f.setNamespaceAware(true);
			XmlPullParser xmlPullParser = f.newPullParser();

			xmlPullParser.setInput(new StringReader(SAXTest.xml));
			StringBuilder tab = new StringBuilder();
			int eventType = xmlPullParser.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {
				if (eventType == XmlPullParser.START_DOCUMENT) {
					// 初始化 root linklist
					root = new Data();
					// 初始话栈
					stack = new LinkedList<Data>();
					// 把root进栈
					stack.addFirst(root);
				} else if (eventType == XmlPullParser.START_TAG) {
					tab.append("\t");
					String s = tab + "<" + xmlPullParser.getName();

					// 创建自己
					self = new Data();
					self.name = xmlPullParser.getName();
					// 获取父对象这里应该 出栈后 在入栈 但是用 linkedList 直接获取栈顶数据 就不用 进行出入栈操作了
					parent = stack.peekFirst();
					// 自己进栈
					stack.addFirst(self);
					// 把自己添加到parent里面去
					addSelfInParent(self, parent, xmlPullParser.getName());
					// 填充属性
					int count = xmlPullParser.getAttributeCount();
					if (count > 0) {
						self.attr = new HashMap<String, String>();
					}
					for (int i = 0; i < count; i++) {
						s += " " + xmlPullParser.getAttributeName(i) + "="
								+ xmlPullParser.getAttributeValue(i);
						self.attr.put(xmlPullParser.getAttributeName(i),
								 xmlPullParser.getAttributeValue(i));
					}

				} else if (eventType == XmlPullParser.END_TAG) {
					// 解析完后自己出栈
					stack.removeFirst();
					self=stack.getFirst();
				} else if (eventType == XmlPullParser.TEXT) {
					self.data=xmlPullParser.getText();

				}
				try {
					eventType = xmlPullParser.next();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			stack = null;
			parent = null;
			self = null;

		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println(root);

	}

	/**
	 * 
	 * @param self
	 * @param parent
	 * @param key
	 */
	@SuppressWarnings("unchecked")
	private static void addSelfInParent(Data self, Data parent, String key) {
		// TODO Auto-generated method stub

		if (parent.son == null) {
			parent.son = new HashMap<String, ArrayList<Data>>();
			ArrayList<Data> d = new ArrayList<Data>();
			parent.son.put(key, d);
			d.add(self);
		} else {
			if (parent.son.containsKey(key)) {
				parent.son.get(key).add(self);
			} else {
				ArrayList<Data> d = new ArrayList<Data>();
				parent.son.put(key, d);
				d.add(self);
			}
		}
	}
}
